package com.erp.products.productlist.controller;

import com.alibaba.excel.EasyExcel;
import com.erp.common.annotation.Log;
import com.erp.common.core.controller.BaseController;
import com.erp.common.core.domain.AjaxResult;
import com.erp.common.core.page.TableDataInfo;
import com.erp.common.enums.BusinessType;
import com.erp.common.utils.poi.ExcelUtil;
import com.erp.products.productlist.domain.ErpProducts;
import com.erp.products.productlist.listener.MdUnitMeasureDataListener;
import com.erp.products.productlist.service.ProductListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.erp.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/product")
public class ProductListController extends BaseController {
    @Autowired
    private ProductListService productListService;


    @GetMapping("/list")
    public TableDataInfo list(ErpProducts erpProducts) {
        startPage();
        List<ErpProducts> list = productListService.list(erpProducts);
        return getDataTable(list);
    }

    /**
     * 查询商品详情
     */
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return AjaxResult.success(productListService.selectErpProductsById(productId));
    }


    /**
     * 删除商品
     */
    @GetMapping("/delete/{productId}")
    public AjaxResult del(@PathVariable("productId") Long productId)
    {
        return AjaxResult.success(productListService.deleteErpProductsById(productId));
    }

    /**
     * 修改商品
     */
    @GetMapping("/update")
    public AjaxResult update(ErpProducts erpProducts) {
        return AjaxResult.success(productListService.updateErpProducts(erpProducts));
    }

    /**
     * 导入单位
     */
    @PostMapping("/import")
    @Log(title = "计量单位管理", businessType = BusinessType.INSERT)
    public AjaxResult importUnit(@RequestParam("file") MultipartFile file) {
        try {
            // 重点,此处第三个参数需要传入Service对象
            EasyExcel.read(file.getInputStream( ),
                            ErpProducts.class,
                            new MdUnitMeasureDataListener(productListService))
                    .sheet( ).doRead( );
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 导出商品信息列表
     */
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpProducts erpProducts)
    {
        List<ErpProducts> list = productListService.list(erpProducts);
        ExcelUtil<ErpProducts> util = new ExcelUtil<ErpProducts>(ErpProducts.class);
        util.exportExcel(response, list, "商品信息数据");
    }
}
